#!/usr/bin/python
import unittest

###
#Todo: Needs a better name!
###

class EnteredTextParserAndExecutor:
	"""Description"""
		
	def __init__(self):
		self.defaultActions = [
			['$$', self.execute], 
			['$', self.executeAsSuperUser], 
			['=', self.calculate]]
	
	def parseAndExecute(self, text):
		for action in self.defaultActions:
			if text.startswith(action[0]):
				return action[1](text[len(action[0]):])
				
	def execute(self, text):
		executeString = text.strip().split()
		print executeString
		Execute(None, executeString)
		
	def executeAsSuperUser(self, text):
		executeString = ['gksudo'] + text.strip().split()
		print executeString
		Execute(None, executeString)
		
	def calculate(self, text):
		result = ''
		try:
			result = str(eval(text,{},{}))
		except:
			pass
		return '=' + result

class EnteredTextParserAndExecutorTest(unittest.TestCase):
	def testParseAndExecute(self):
		parser = EnteredTextParserAndExecutor()
		result = parser.parseAndExecute('=1+1')
		self.assertEquals(result, '=2')
		
if __name__ == "__main__":
	 unittest.main()
